import ChatModel from "~/models/ChatModel";
import SessionModel from "~/models/SessionModel";
import { SessionType } from "~/types/chat";

export const findChatsBySessionId = async (sessionId: string) => {
    const list = await ChatModel.find({ session_id: sessionId });

    return { list };
};

export const createChats = async (sessionId: string, chats: { msg: string, time: number }[]) => {
    await ChatModel.insertMany(chats.map((c, i) => ({ session_id: sessionId, role: i % 2 ? 'bot' : 'user', content: c.msg, create_time: c.time })));
};

export const updateSessionById = async (sessionId: string, session: Partial<SessionType>) => {
    await SessionModel.findByIdAndUpdate(sessionId, session);
};

export const createSession = async (userId: string) => {
    const session = await SessionModel.create({ user_id: userId, create_time: +new Date() });

    return session;
};